package cn.edu.hrbcu.es.invigilatemanagerment.controller;

import cn.edu.hrbcu.es.invigilatemanagerment.pojo.Permission;
import cn.edu.hrbcu.es.invigilatemanagerment.pojo.Role;
import cn.edu.hrbcu.es.invigilatemanagerment.pojo.RolePermission;
import cn.edu.hrbcu.es.invigilatemanagerment.service.IPermissionService;
import cn.edu.hrbcu.es.invigilatemanagerment.service.IRolePermissionService;
import cn.edu.hrbcu.es.invigilatemanagerment.service.IRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Slf4j
@RequestMapping("/RolePermission")
public class RolePermissionController {
    @Autowired
    private IRolePermissionService rolePermissionService;
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IPermissionService permissionService;
    @RequestMapping("queryAllRolePermission")
    public String queryAllRolePermission(Model model,@RequestParam(value = "pageNow",defaultValue = "1")int pageNow,
                                         @RequestParam(value = "pageSize",defaultValue = "5")int pageSize) {
        int totalCount = rolePermissionService.queryRolePermissionCount();
        int pageCount = (int)Math.ceil((double)totalCount/pageSize);
        List<RolePermission> rolePermissions = rolePermissionService.queryRolePermissionByPage(pageNow,pageSize);
        List<Map<String,Object>> list = new ArrayList<>();
        for(RolePermission rolePermission:rolePermissions)
        {
            Map<String,Object> map = new HashMap<>();
            String roleName = roleService.queryRoleById(rolePermission.getRoleid()).getName();
            log.info("rolePermission.getPermissionid()"+rolePermission.getPermissionid());
            String permissionName = permissionService.queryPermissionById(rolePermission.getPermissionid()).getDescription();
            map.put("id",rolePermission.getId());
            map.put("roleName",roleName);
            map.put("permissionName",permissionName);
            list.add(map);
        }
        model.addAttribute("rolePermissions", list);
        model.addAttribute("pageCount",pageCount);
        model.addAttribute("pageNow",pageNow);
        model.addAttribute("pageSize",pageSize);
        return "rolepermissions";
    }
    @RequestMapping("toAddRolePermission")
    public String toAddRolePermission(Model model) {
        List<Role> roles = roleService.queryAllRole();
        List<Permission> permissions = permissionService.queryAllPermission();
        model.addAttribute("roles",roles);
        model.addAttribute("permissions",permissions);
        return "toaddrolepermission";
    }

    @RequestMapping("toUpdateRolePermission")
    public ModelAndView toUpdateRolePermission(Integer id)
    {
        RolePermission rolePermission = rolePermissionService.queryRolePermissionsById(id);
        List<Role> roles = roleService.queryAllRole();
        List<Permission> permissions = permissionService.queryAllPermission();
        ModelAndView model = new ModelAndView("tpupdaterolepermission");
        model.addObject("rolePermission",rolePermission);
        model.addObject("roles",roles);
        model.addObject("permissions",permissions);
        return model;
    }
    @RequestMapping("updateRolePermissionById")
    @ResponseBody
    public String updateRolePermissionById(RolePermission rolePermission)
    {
        rolePermissionService.updateRolePermissionByPrimaryId(rolePermission);
        return "OK";
    }

    @RequestMapping("insertRolePermission")
    @ResponseBody
    public String insertRolePermission(RolePermission rolePermission)
    {
        rolePermissionService.insertRolePermission(rolePermission);
        return "OK";
    }
    @RequestMapping("deleteRolePermissionById")
    @ResponseBody
    public String deleteRolePermissionById(Integer id)
    {
        rolePermissionService.deleteRolePermissionByPrimaryId(id);
        return "OK";
    }
}
